package com.ctc.ctcoss.ws.service;

import com.fadadianqi.commons.ConfigHelper;

import java.security.MessageDigest;
import java.util.Calendar;
import java.util.List;

public class WebServiceSendSms {
    /**
     * WebService地址
     */
    // private static final String url =
    // "http://www.10690300.com/services/SendSms?wsdl"; //三网通账号使用地址
    //private static final String url = "http://3tong.net/services/SendSms?wsdl"; // 无限通账号使用地址

    private static final String url = "http://www.10690300.com/services/SendSms?wsdl";
    private static final String ID = "dh20335";
    private static final String PW = "dh20335.com";
    private static final String phone = "13116172528";
    private static final String content = "我在调试接口";

    //private static String[] phoneArr = { "13116172528" };// {"18621803633","18221493849","15000167215"};//
    //private static String[] contentArr = { "test1" };

    private static final String getID() {
        return ConfigHelper.getString("3tong.net.id", ID);
    }

    private static final String getPW() {
        return ConfigHelper.getString("3tong.net.pw", PW);
    }

    /**
     * @param args
     */
    @SuppressWarnings("static-access")
    public static void main(String[] args) {
        // 发送短信,支持内容相同的短信群发 f55967d71d434672a0ffd7622c08b3f4
        String[] sms_result = sendSMS(ID, PW, phone, content);
        if (sms_result != null) {
            for (int i = 0; i < sms_result.length; i++) {
                System.out.println("String[" + i + "]:  " + sms_result[i]);
            }
        }
        // // ***********发送短信,支持内容不相同的批量群发****************
        // String[] sms_BatchSendSms = BatchSendSms(phoneArr, contentArr);
        // if (sms_BatchSendSms != null) {
        // for(int i=0;i<sms_BatchSendSms.length;i++){
        // System.out.println("String["+i+"]:  "+sms_BatchSendSms[i]);
        // }
        // }
        // // 线程休眠10秒钟，保证所发短信全部入库
        // try {
        // Thread.currentThread().sleep(60000);
        // } catch (Exception e) {
        // e.printStackTrace();060bb33bdb7f4e249634457f62f661de
        // }
        // ***************获取短信状态报告,返回指定smsId的状态报告*************
        /*
		 * List<String> smsIds = new ArrayList<String>();
		 * smsIds.add("060bb33bdb7f4e249634457f62f661de"); String[]
		 * sms_GetReport = GetReport(smsIds); if (sms_GetReport != null) { for
		 * (int i = 0; i < sms_GetReport.length; i++) {
		 * System.out.println("String[" + i + "]:  " + sms_GetReport[i]); } }
		 */

        // //******* 获取上行短信,一次最多返回100条*******************
		/*
		 * String[] sms_GetSms = GetSms(); if (sms_GetSms != null) { for(int
		 * i=0;i<sms_GetSms.length;i++){
		 * System.out.println("String["+i+"]:  "+sms_GetSms[i]); } }
		 */
        // //****************获取账户余额***********************
        // String[] sms_GetBalance = GetBalance();
        // if (sms_GetBalance != null) {
        // for(int i=0;i<sms_GetBalance.length;i++){
        // System.out.println("String["+i+"]:  "+sms_GetBalance[i]);
        // }
        // }

    }

    public static String[] sendSMS(String phone, String content) {
        return sendSMS(getID(), getPW(), phone, content);
    }

    public static String[] sendSMS(String account, String password,
                                   String phone, String content) {
        ISmsServiceProxy proxy = new ISmsServiceProxy(url);
        String[] sms_result = null;
        try {
            // 创建一个内容相同的多个号码的短信对象，进行群发
            MtNewMessage message = new MtNewMessage();
            message.setPhoneNumber(phone);
            message.setContent(content);
            message.setScheduleTime(Calendar.getInstance());
            sms_result = proxy.sendSms(account, MD5Encode(password), message);
            System.out.println("********短信下行*******");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sms_result;

    }

    /**
     * 号码不同，内容相同的短信群发。
     *
     * @param phoneNumber
     * @param content
     * @return
     */
    public static String[] batchSendSMS(String[] phoneNumber, String content) {
        ISmsServiceProxy proxy = new ISmsServiceProxy(url);
        String[] sms_result = null;
        try {
            // 创建内容不同的多个号码的短信对象，进行群发
            MtNewMessage[] messages = new MtNewMessage[phoneNumber.length];
            for (int i = 0; i < phoneNumber.length; i++) {
                MtNewMessage message = new MtNewMessage();
                message.setPhoneNumber(phoneNumber[i]);
                message.setContent(content);
                messages[i] = message;
            }
            sms_result = proxy.batchSendSms(getID(), MD5Encode(getPW()), messages);
            System.out.println("********短信群发*******");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sms_result;
    }

    public static String[] BatchSendSms(String[] phoneNumber, String[] content) {
        ISmsServiceProxy proxy = new ISmsServiceProxy(url);
        String[] sms_result = null;
        try {
            // 创建内容不同的多个号码的短信对象，进行群发
            MtNewMessage[] messages = new MtNewMessage[phoneNumber.length];
            for (int i = 0; i < phoneNumber.length; i++) {
                MtNewMessage message = new MtNewMessage();
                message.setPhoneNumber(phoneNumber[i]);
                message.setContent(content[i]);
                messages[i] = message;
            }
            sms_result = proxy.batchSendSms(getID(), MD5Encode(getPW()),
                    messages);
            System.out.println("********短信群发*******");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sms_result;
    }

    public static String[] GetSms() {
        ISmsServiceProxy proxy = new ISmsServiceProxy(url);
        String[] sms_result = null;
        try {
            sms_result = proxy.getSms(getID(), MD5Encode(getPW()));
            System.out.println("********短信上行*******");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sms_result;
    }

    public static String[] GetReport(List<String> smsIds) {
        ISmsServiceProxy proxy = new ISmsServiceProxy(url);
        String[] sms_result = null;
        try {
            MtNewMessage[] messages = new MtNewMessage[smsIds.size()];
            MtNewMessage message = null;
            for (int i = 0; i < smsIds.size(); i++) {
                message = new MtNewMessage();
                message.setSmsId(smsIds.get(i));
                messages[i] = message;

            }
            sms_result = proxy.getReport(getID(), MD5Encode(getPW()), messages);
            System.out.println("********下行状态*******");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sms_result;
    }

    public static String[] GetBalance() {
        ISmsServiceProxy proxy = new ISmsServiceProxy(url);
        String[] sms_result = null;
        try {
            sms_result = proxy.getBalance(getID(), MD5Encode(getPW()));
            System.out.println("********账户余额*******");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sms_result;
    }

    // MD5加密函数
    public static String MD5Encode(String sourceString) {
        String resultString = null;
        try {
            resultString = new String(sourceString);
            MessageDigest md = MessageDigest.getInstance("MD5");
            resultString = byte2hexString(md.digest(resultString.getBytes()));
        } catch (Exception ex) {
        }
        return resultString;
    }

    public static final String byte2hexString(byte[] bytes) {
        StringBuffer bf = new StringBuffer(bytes.length * 2);
        for (int i = 0; i < bytes.length; i++) {
            if ((bytes[i] & 0xff) < 0x10) {
                bf.append("0");
            }
            bf.append(Long.toString(bytes[i] & 0xff, 16));
        }
        return bf.toString();
    }
}
